import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from 'vuex-persist';
import System from './modules/system/index';
import WorkflowCenter from './modules/workflow-center/index';
import { bizproperty, bizpropertyApi } from "@cloudpivot/api";
import { listApi }  from '@cloudpivot/api';


const vuexLocal = new VuexPersistence({
  key: 'vuex',
  storage: localStorage,
  reducer: (state: any) => (
    {
      appCode: state.appCode
    })
});

Vue.use(Vuex);

export default new Vuex.Store({
  namespace: true,
  state: {
    appCode: '', // 单应用appCode,
    config: {},
    formRuleList: [],
    singleAppGroups:[]//singleApp 菜单
  },
  mutations: {
    setAppGroups(state: any, singleAppGroups: Array<any>){
      state.singleAppGroups = null;
      state.singleAppGroups = singleAppGroups;
    },
    setAppCode(state: any, appCode: string) {
      state.appCode = appCode;
    },
    setConfig(state: any, config: string) {
      state.config = config;
    },
    setFormRuleList(state: any, val: Array<any>) {
      state.formRuleList = val;
    },
  },
  actions: {
    async getAppGroups({commit, state}: any, param?: any) {
      const res = await listApi.getFolder(param);
      if (res.errcode === 0) {
        if (!Array.isArray(res.data)) {
          return;
        }
        commit('setAppGroups', res.data);
      }
    },
    async getFormRuleList({commit, state}: any, param?: any) {
      const params: bizproperty.dataRlueParam = {
        schemaCode: param.bizSchemaCode
      };
      const res = await bizpropertyApi.getDataRules(params);
      if (res.errcode === 0) {
        commit('setFormRuleList', res.data);
      } 
    },
  },
  modules: {
    WorkflowCenter,
    System
  },
  plugins: [vuexLocal.plugin]
} as any);
